﻿/**
 * RectEmitter
 *
 * @author		Allen Chou
 * @version 	1.1.1 (last update: Aug 26 2008)
 * @link        http://cjcat.blogspot.com
 * @link		http://cjcat2266.deviantart.com
 */

/*
 * Copyright 2008 (C) Allen Chou (cjcat2266@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package idv.cjcat.display.particles {
	import flash.display.*;
	import flash.events.*;
	
	import idv.cjcat.*;
	import idv.cjcat.display.*;
	import idv.cjcat.geom.*;
	
	public class RectEmitter extends AbstractEmitter {
		
		public  var centerBiasIter:uint = 0;
		
		private var _borderL:Number = 70;
		private var _borderR:Number = 70;
		private var _borderT:Number = 30;
		private var _borderB:Number = 30;
		
		public function RectEmitter(displayClass:Class = null, container:DisplayObjectContainer = null) {
			super(displayClass, container);
		}
		
		override public function spawn():DisplayObject {
			var temp:DisplayObject = new displayClass();
			var slopeAng:Number = Math.atan2(normal.y, normal.x) + Math.PI / 2;
			var randPos:Number = CJMath.pseudoGaussian(0.5, 0.5, centerBiasIter + 1);
			var randPos2:Number = CJMath.pseudoGaussian(0.5, 0.5, centerBiasIter + 1);
			temp.x = centerX + CJMath.interpolate(0, -_borderL * Math.cos(slopeAng), 1, _borderR * Math.cos(slopeAng), randPos);
			temp.y = centerY + CJMath.interpolate(0, -_borderL * Math.sin(slopeAng), 1, _borderR * Math.sin(slopeAng), randPos);
			temp.x -= CJMath.interpolate(0, -_borderT * Math.sin(slopeAng), 1, _borderB * Math.sin(slopeAng), randPos2);
			temp.y += CJMath.interpolate(0, -_borderT * Math.cos(slopeAng), 1, _borderB * Math.cos(slopeAng), randPos2);
			return temp;
		}
		
		override public function getConfig(mask:EmitterConfigMask = null):Object {
			if (mask == null) mask = new EmitterConfigMask();
			var obj:Object = super.getConfig(mask);
			obj.centerViasIter = this.centerBiasIter;
			obj.borderL = this.borderL;
			obj.borderR = this.borderR;
			obj.borderT = this.borderT;
			obj.borderB = this.borderB;
			
			return obj;
		}
		
		//accessors
		//------------------------------------------------------------------------------------------------
		
		override public function set showAuxShape(value:Boolean):void {
			if (_auxShape != null) removeChild(_auxShape);
			_showAuxShape = value;
			if (value) {
				_auxShape = new Shape();
				CJDisplay.setLineStyle(_auxShape, auxShapeLineStyle);
				_auxShape.graphics.moveTo(0, 0);
				_auxShape.graphics.lineTo(0, -30);
				_auxShape.graphics.lineTo( 10, -15);
				_auxShape.graphics.lineTo(-10, -15);
				_auxShape.graphics.lineTo(0, -30);
				_auxShape.graphics.moveTo(-_borderL, -_borderT);
				_auxShape.graphics.lineTo(_borderR, -_borderT);
				_auxShape.graphics.lineTo(_borderR, _borderB);
				_auxShape.graphics.lineTo(-_borderL, _borderB);
				_auxShape.graphics.lineTo( -_borderL, -_borderT);
				updateAuxShape();
				addChild(_auxShape);
			} else {
				if (_auxShape != null) removeChild(_auxShape);
				_auxShape = null;
			}
		}
		
		override public function updateAuxShape(e:Event = null):void {
			if (_auxShape != null) {
				_auxShape.rotation = 90 + (Math.atan2(_normal.y, _normal.x) / Math.PI * 180);
				_auxShape.x = centerX;
				_auxShape.y = centerY;
			}
		}
		
		public function get lengthH():Number { return _borderL + _borderR; }
		public function set lengthH(value:Number):void {
			_borderL = _borderR = value / 2;
		}
		
		public function get lengthV():Number { return _borderT + _borderB; }
		public function set lengthV(value:Number):void {
			_borderT = _borderB = value / 2;
		}
		
		public function get borderL():Number { return _borderL; }
		public function set borderL(value:Number):void {
			_borderL = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		public function get borderR():Number { return _borderR; }
		public function set borderR(value:Number):void {
			_borderR = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		public function get borderT():Number { return _borderT; }
		public function set borderT(value:Number):void {
			_borderT = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		public function get borderB():Number { return _borderB; }
		public function set borderB(value:Number):void {
			_borderB = CJMath.clamp(value, 0, Number.MAX_VALUE);
			if (showAuxShape) showAuxShape = true;
		}
		
		//end of accessors
		//------------------------------------------------------------------------------------------------
		
	}
}